/**
 * Multicast project Bidois Morgan & Thomas Nunes
 *
 */
package middleware;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Morgan Bidois <morgan.bidois@e.ujf-grenoble.fr>
 * 
 * the directory regroup all infos of each client of the group
 */
public class Directory {

    private ArrayList<ClientInfos> clientList;

    public Directory() {
        this.clientList = new ArrayList<>();
    }

    public void addClient(ClientInfos newClient) {
        this.clientList.add(newClient);
    }
    
    public List<ClientInfos> getClientList(){
        return this.clientList;
    }

    public ClientInfos getClientById(int id) {
        ClientInfos ciToReturn = null;
        for (ClientInfos ci : clientList){
            if (ci.getIdClient() == id){
                ciToReturn = ci;
                break;
            }
        }
        return ciToReturn;
    }
    
    public ClientInfos getClient(int index) {
        return this.clientList.get(index);
    }
    
    /**
     * Serialize directory
     * 
     * @return String result.
     */
    public String getDirectoryToSend() {
        String str = "";
        for (ClientInfos ci : this.clientList) {
            str += ci.getClientToSend();
        }
        return str;
    }
    
    
    @Override
    public String toString() {
        String str = "";
        for (ClientInfos ci : this.clientList) {
            str += "\n" + ci.toString();
            if (this.clientList.size() != this.clientList.indexOf(ci)) {
                str += "\n----------------";
            }
        }
        return str;
    }
}
